﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TraceabilityApp_Infrastructure
{
    public class Base<T>:IBase<T> where T : class
    {
        private readonly MyDbContext my;

        public Base(MyDbContext my)
        {
            this.my = my;
        }
        public async Task<int> Add(T t)
        {
            my.Set<T>().Add(t);
            return await my.SaveChangesAsync();
        }

        //public async Task<int> AddAsync(List<T> t)
        //{
        //    my.Set<T>().AddRangeAsync(t);
        //    return await my.SaveChangesAsync();
        //}

        public async Task<int> Del(T t)
        {
            my.Set<T>().Remove(t);
            return await my.SaveChangesAsync();
        }

        //public async Task<int> Delete(List<T> t)
        //{
        //    my.Set<T>().RemoveRange(t);
        //    return await my.SaveChangesAsync();
        //}

        public IQueryable<T> GetAll()
        {
            return my.Set<T>();
        }

        public async Task<T> GetValue(int id)
        {
            return await my.Set<T>().FindAsync(id);
        }

        public Task<int> Update(T t)
        {
            my.Set<T>().Update(t);
            return my.SaveChangesAsync();
        }
    }
}
